<template>
  <div>
    <el-dialog v-model="dialog_info.isShow" :title="dialog_info.isAdd ? '添加菜单' : '编辑菜单'" center>
      <el-form :model="menuForm" :rules="rules" ref="formRef">
        <el-form-item prop="title" label="菜单名称" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="menuForm.title" />
        </el-form-item>
        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="menuForm.pid" class="m-2" placeholder="Select" size="large">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in store.getters['menu/getMenu']" :key="item.id" :label="item.title"
              :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="menuForm.type" :label="1" size="large">目录</el-radio>
          <el-radio v-model="menuForm.type" :label="2" size="large">菜单</el-radio>
        </el-form-item>=
        <el-form-item label="菜单图标" :label-width="formLabelWidth">
          <el-input v-model="menuForm.icon" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="菜单地址" :label-width="formLabelWidth">
          <el-input v-model="menuForm.url" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="menuForm.status" style="--el-switch-on-color: #13ce66;
            --el-switch-off-color: #ff4949;" :active-value="1" :inactive-value="2" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button @click="confirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, defineProps, defineEmits, defineExpose } from "vue";
import { FormInstance, FormRules, ElMessage } from "element-plus";
import { addMenu, menuInfo, menuEdit } from "../../request/api";
import { useStore } from "vuex";
let store = useStore();
//接收父组件的属性
let props = defineProps({
  dialog_info: Object,
});
let emit = defineEmits(["cancel"]);
//label宽度
let formLabelWidth = ref("120px");
let formRef = ref<FormInstance>();
//创建一个表单对象
let menuForm = ref({
  pid: 0, //上级分类编号  注意：顶级菜单-0number
  title: "", //菜单名称string
  icon: "", //图标string
  type: 1, //类型 1目录2菜单number
  url: "", //菜单地址string
  status: 1, //状态   1正常2禁用   number类型number
});
//规则验证
let rules = reactive<FormRules>({
  title: [
    { required: true, message: "请输入菜单名称", trigger: "blur" },
    { min: 2, max: 6, message: "Length should be 2 to 6", trigger: "blur" },
  ],
  pid: [
    {
      required: true,
      message: "Please select pid",
      trigger: "change",
    },
  ],
});
//封装重置事件
let reset = () => {
  menuForm.value = {
    pid: 0, //上级分类编号  注意：顶级菜单-0number
    title: "", //菜单名称string
    icon: "", //图标string
    type: 1, //类型 1目录2菜单number
    url: "", //菜单地址string
    status: 1, //状态   1正常2禁用   number类型number
  };
  //清空验证规则
  formRef.value?.resetFields();
};
//封装一个取消事件
let cancel = () => {
  emit("cancel", false);
  reset();
};
//封装一个查询一条数据事件
let lookup = (id: number) => {
  menuInfo({ id }).then((res: any) => {
    if (res.data.code == 200) {
      menuForm.value = res.data.list;
      menuForm.value.id = id;
      // console.log(menuForm,'menuForm')
    } else {
      ElMessage.error(res.data.msg);
    }
  });
  // console.log(id,'编辑id')
};

//封装一个确定事件
const confirm = () => {
  formRef.value?.validate((val) => {
    if (val) {
      //console.log(menuForm, "表单对象");
      if (props.dialog_info.isAdd) {
        //调用添加接口
        addMenu(menuForm.value).then((res: any) => {
          console.log(res, "rrrr");
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg);
            //关闭弹框
            cancel();
            //重现调用接口
            store.dispatch("menu/changeMenuAction");
          } else {
            ElMessage.error(res.data.msg);
          }
        });
      } else {
        //调用编辑接口
        menuEdit(menuForm.value).then((res: any) => {
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg);
            //关闭弹框
            cancel();
            //重现调用接口
            store.dispatch("menu/changeMenuAction");
          } else {
            ElMessage.error(res.data.msg);
          }
        });
      }
    }
  });
};

//导出ref需要的函数或者属性
defineExpose({
  lookup,
});
</script>

<style scoped></style>
